package math
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * 内摆线 
	 * @author KimWang
	 * 方程:
	 * x=cosθ+cos(nθ)/n
	 * y=sinθ-sin(nθ)/n
	 * 的曲线，其中 n 为正实数。
	 */
	public class Hypocycloid extends Sprite
	{
		private var n:Number = 2;
		private var radius:Number = 100;
		private var pen:Sprite=new Sprite();
		private var lineMc:Sprite=new Sprite();
		private var angle:int=-1;//角度
		private var centerX:Number = 0;//中心点x位置
		private var centerY:Number = 0;//中心点y位置
		public function Hypocycloid(centerX:Number, centerY:Number, stage:Stage)
		{
			this.centerX = centerX;
			this.centerY = centerY;
			init(stage);
		}
		
		private function init(stage:Stage):void
		{
			addChild(pen);
			addChild(lineMc);
			
			//描绘十字坐标
			this.drawCoordinate();
			
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
			stage.addEventListener(MouseEvent.CLICK, mouseClickHandler);
			stage.addEventListener(Event.ENTER_FRAME,Run);
		}
		
		/** 绘制十字坐标*/
		private function drawCoordinate():void
		{
			pen.graphics.lineStyle(1);
			pen.graphics.moveTo(centerX,centerY);
			pen.graphics.lineTo(centerX,0);
			
			pen.graphics.moveTo(centerX,centerY);
			pen.graphics.lineTo(centerX*2,centerY);
			
			pen.graphics.moveTo(centerX,centerY);
			pen.graphics.lineTo(centerX,centerY*2);
			
			pen.graphics.moveTo(centerX,centerY);
			pen.graphics.lineTo(0,centerY);
		}
		
		private function Run(evt:Event):void
		{
			lineMc.graphics.clear();
			if (angle==-1)
			{
				var px:Number=centerX+radius * (Math.cos(angle*Math.PI/180) + Math.cos(angle*Math.PI/180 * n) / n);
				var py:Number=centerY+radius * (Math.sin(angle*Math.PI/180) - Math.sin(angle*Math.PI/180 * n) / n);
				lineMc.graphics.lineStyle(1,0x0033FF);
				lineMc.graphics.moveTo(centerX,centerY);
				lineMc.graphics.lineTo(px,py);
				lineMc.graphics.drawCircle(px,py,5);
				
				pen.graphics.moveTo(px,py);
				drawLine(px,py);
			}
			else
			{
				px =centerX+radius * (Math.cos(angle*Math.PI/180) + Math.cos(angle*Math.PI/180 * n) / n);
				py =centerY+radius * (Math.sin(angle*Math.PI/180) - Math.sin(angle*Math.PI/180 * n) / n);
				lineMc.graphics.lineStyle(1,0x0033FF);
				lineMc.graphics.moveTo(centerX,centerY);
				lineMc.graphics.lineTo(px,py);
				lineMc.graphics.drawCircle(px,py,5);
				drawLine(px,py);
				
			}
			angle++;
		}
		//绘制线程序
		private function drawLine(x2:Number ,y2:Number):void
		{
			pen.graphics.lineStyle(1,0xff0000);
			pen.graphics.lineTo(x2,y2);
		}
		
		private function mouseWheelHandler(evt:MouseEvent):void
		{
			pen.graphics.clear();
			drawCoordinate();
			stage.addEventListener(Event.ENTER_FRAME,Run);
			angle = -1;
			n += Math.E;
		}
		
		private function mouseClickHandler(evt:MouseEvent):void
		{
			pen.graphics.clear();
			drawCoordinate();
			stage.addEventListener(Event.ENTER_FRAME,Run);
			angle = -1;
			n++;
		}
	}
}